<template>
	<view class="content" :style="'height:'+height">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist" :key="item.id" :style="{'--main-bg-color':main_clor}" class="f-item b-b" :class="{active: item.id === currentId}"
				@click="tabtap(item)">
				{{item.name}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll"
			:scroll-top="tabScrollTop">
		 
			<view v-for="item in slist" :key="item.id" class="s-list" :id="'main-'+item.id">
				<text class="s-item" :style="{'--main-bg-color':main_clor}">{{item.name}}</text>
				<view class="t-list">
					<!-- v-if改v-show适配vue3 -->
					<view @click="navToList(titem)" v-show="titem.pid === item.id" class="t-item" v-for="titem in tlist"
						:key="titem.id">
						<image style="border-radius: 16rpx;" :src="titem.picture"></image>
						<text style="position: relative;top: 5rpx;color: #545454;">{{titem.name}}</text>
					</view>
				</view>
			</view>
			<view v-if="slist == 0" style="position: relative;top: 20%;">
				<u-empty
				        mode="message"
				        icon="https://xiangyou-1313406647.cos.ap-nanjing.myqcloud.com/test/1738749101_空数据.png"
				>
				</u-empty>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props: {
			// 第一级数组
			flist: {
				type: Array,
				default: []
			},
			// 第二级数组
			slist: {
				type: Array,
				default: []
			},
			// 第三级数组
			tlist: {
				type: Array,
				default: []
			},
			main_clor:{
				type:String,
				default: '#dd524d'
			},
			height:{
				type:String,
				default: '600px'
			},
			currentId:{
				type:Number,
				default: 1
			},


		},
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
			 

			}
		},

		methods: {

			//一级分类点击
			tabtap(item) {
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				// this.currentId = item.id;
				this.$emit('currentIdClick',item)
				 
				let index = this.slist.findIndex(sitem => sitem.pid === item.id);
				// this.tabScrollTop = this.slist[index].top;
			},
			//右侧栏滚动
			asideScroll(e) {
				console.log(e)
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item => item.top <= scrollTop).reverse();
				if (tabs.length > 0) {
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				this.slist.forEach(item => {
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(item) {

				this.$emit("itemClick", item);

			}
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}

	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color: #fff;
	}

	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: #444444;
		position: relative;

		&.active {
			color: var(--main-bg-color);
			background: #f8f8f8;

			&:before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: #f1f2f3;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside {
		flex: 1;
		overflow: hidden;
		padding: 0 5px;
		/* overflow-y: scroll; */
	}

	.s-item {
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: 28upx;
		font-weight: 700;
		color: var(--main-bg-color);
	}

	.t-list {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		padding-top: 12upx;

		&:after {
			content: '';
			flex: 99;
			height: 0;
		}
	}

	.t-item {
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 176upx;
		font-size: 26upx;
		color: #444444;
		padding-bottom: 20upx;

		image {
			width: 140upx;
			height: 140upx;
		}
	}
</style>